package collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * List的集合
 */
public class ListDemo {
    public static void main(String[] args) {
        LinkedList<String>list=new LinkedList<>();
        list.add("one");
        list.add("two");
        list.add("three");
        list.add("four");


        String e=list.get(2);//获取相应下标的元素
        System.out.println(e);

        for (int i = 0; i <list.size() ; i++) {
            System.out.println(list.get(i));
        }

        for (String s:list){
            System.out.println(s);
        }

        Iterator<String>it=list.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }

        System.out.println("-----------------------------------");

        String old=list.set(2,"six");//替换下标为2的元素并返回被替换的元素
        System.out.println("list:"+list );
        System.out.println(old);

        String a=list.remove(2);//删除下标为2的元素，并返回被删元素
        System.out.println("a = " + a);
        System.out.println("list = " + list);


        /*
        void add(int index,E e):
        将给定元素e添加到index所指定的位置，相当于插入
         */
        list.add(2,"seven");//在list下标为2的位置插入“seven”
        list.add("123");
        System.out.println("list = " + list);

    }
}
